import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.JFileChooser;

class MenuBarTest extends JFrame implements ActionListener{
	JTextArea workArea=new JTextArea(190,290);
	public MenuBarTest(){
	
		super("untitled");
		
		setLayout(new BorderLayout());
		
		JMenuBar menuBar1=new JMenuBar();
		JMenu fileMenu=new JMenu("FILE");
		JMenuItem newMenuItem=new JMenuItem("New");
		JMenuItem openMenuItem=new JMenuItem("Open");
		JMenuItem closeMenuItem=new JMenuItem("Close");
		
		menuBar1.add(fileMenu);
		fileMenu.add(newMenuItem);
		fileMenu.add(openMenuItem);
		fileMenu.add(closeMenuItem);
		
		add(menuBar1,BorderLayout.NORTH);
		add(workArea,BorderLayout.CENTER);
		
		
		
		newMenuItem.addActionListener(this);
		openMenuItem.addActionListener(this);
		closeMenuItem.addActionListener(this);

		setSize(1200,1000);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public void actionPerformed(ActionEvent avt){
		String cmd=avt.getActionCommand();
		if(cmd.equals("Open")){	
			try{
		
				JFileChooser chooser=new JFileChooser();
				int val;
				val=chooser.showOpenDialog(null);
				if(JFileChooser.APPROVE_OPTION==val){
					File f=chooser.getSelectedFile();
					setTitle(chooser.getSelectedFile().getName());
					BufferedReader reader=new BufferedReader(new FileReader(f));
					workArea.setText("");
					String s=reader.readLine();
					while(reader.readLine()!=null){	
						workArea.append(s+"\n");
						s=reader.readLine();
						}
					}
			
				}catch(Exception e){
					System.out.println("Exception found");
         			}
			}
			
		if(cmd.equals("New")){
			setTitle("untitled");
			workArea.setText("");
		}		
		
		if(cmd.equals("Close")){
			System.exit(0);
		}
	}
	
	public static void main(String[] args){
	
		MenuBarTest m1=new MenuBarTest();
	}
}
